//
//  MineViewController.m
//  mhome
//
//  Created by YaSha_Tom on 2018/12/4.
//  Copyright © 2018年 刘春雷. All rights reserved.
//

#import "MineViewController.h"
#import "OrderViewController.h"
#import "ProjectListViewController.h"
#import "UIImage+LX.h"
#import "WavesView.h"
#import "NetworkManager.h"
#import "DataManager.h"
#import "PersonModel.h"
#import "PersonInfoViewController.h"
#import "SystemViewController.h"
#define CellIdentifier @"Cell"
@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) WavesView *wavesView;
@property (nonatomic,strong) UIView *iconView;
@property (nonatomic,strong) UIImageView *iconImgView;
@property (nonatomic,strong) UILabel *nickNameLanel;
@property (nonatomic,strong) UILabel *cellPhonelabel;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic, copy) NSArray *iconArray;
@property (nonatomic, copy) NSArray *titleArray;
@property (nonatomic, copy) NSArray *dataSourceArray;
@end

@implementation MineViewController
#pragma mark -- 懒加载
- (NSArray *)iconArray {
    if(!_iconArray) {
        _iconArray = @[@"dingdang",@"gongcheng",@"hetong",@"设计师",@"设置"];
    }
    return _iconArray;
}
- (NSArray *)titleArray {
    if (!_titleArray) {
        _titleArray = @[@"我的订单", @"我的工程", @"我的合同", @"个人信息",@"系统设置"];
    }
    return _titleArray;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 初始化导航栏
    [self setupNavBar];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的";
    //初始化tableview
    [self setupTableHeaderView];
    [self doNetworking];
}
- (void)doNetworking {
    NSString *urlString = [NSString stringWithFormat:@"%@%@",Domain,@"ucenter/getUserInfoByUserId"];
    NSUserDefaults *de = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = @{@"id":[de stringForKey:@"userid"]};
    [[NetworkManager manager] sendPostRequestWithUrl:urlString paramenters:dic success:^(id responseObject) {
        NSLog(@"成功%@",responseObject);
        if([responseObject[@"code"]  isEqual: @1]){
            self.dataSourceArray = [DataManager getPersonInfoData:responseObject];
            [self setData];
        }else{
            [QMUITips showInfo:responseObject[@"msg"] inView:self.view hideAfterDelay:1];
        }
    } failure:^(NSError *error) {
        NSLog(@"失败%@",error);
    }];
}
- (void)setData{
    PersonModel *model = self.dataSourceArray[0];
    NSUserDefaults *de = [NSUserDefaults standardUserDefaults];
    [self.iconImgView  sd_setImageWithURL:[de objectForKey:@"launcherHeadurl"]placeholderImage:[UIImage imageNamed:@"头像"] options:SDWebImageRetryFailed];
    self.nickNameLanel.text = [NSString stringWithFormat:@"昵称:%@ ",[model.nickName isEqualToString:@""]?@"暂无":model.nickName] ;
    self.cellPhonelabel.text = [NSString stringWithFormat:@"手机号：%@",model.mobile] ;
}
#pragma mark -- 初始化导航栏
- (void)setupNavBar {
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage getImageWithColor:[UIColor qmui_colorWithHexString:@"4bccbc"]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)setupTableHeaderView {
    CGFloat tableHeaderViewX = 0;
    CGFloat tableHeaderViewY = 0;
    CGFloat tableHeaderViewW = kSCREEN_WIDTH;
    CGFloat tableHeaderViewH = 160*kHeightScale;
    UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(tableHeaderViewX, tableHeaderViewY, tableHeaderViewW, tableHeaderViewH)];
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = tableHeaderView;
    self.wavesView = [[WavesView alloc] initWithFrame:CGRectMake(tableHeaderViewX, tableHeaderViewY, tableHeaderViewW, tableHeaderViewH)];
    self.wavesView.backgroundColor =[UIColor qmui_colorWithHexString:@"4bccbc"];
    [self.tableView.tableHeaderView addSubview:self.wavesView];
//    //
    _iconView = [[UIView alloc] init];

    self.iconImgView = [[UIImageView alloc] init];
    [_iconView addSubview:_iconImgView];
    [_iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iconView.mas_top).with.offset(0);
        make.centerX.equalTo(_iconView);
        make.size.mas_equalTo(CGSizeMake(59*kWidthScale, 59*kHeightScale));
    }];
    _iconImgView.layer.borderWidth = 1;
    _iconImgView.layer.borderColor = [UIColor qmui_colorWithHexString:@"ffffff"].CGColor;
    _iconImgView.layer.cornerRadius = 59 * 0.5*kHeightScale;
    _iconImgView.layer.masksToBounds = YES;
    _iconImgView.image = [UIImage imageNamed:@"头像"];

    self.nickNameLanel = [[UILabel alloc] init];
    [_iconView addSubview:self.nickNameLanel];
    [self.nickNameLanel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iconImgView.mas_bottom).with.offset(8);
        make.centerX.equalTo(_iconView);
        make.size.mas_equalTo(CGSizeMake(200*kWidthScale, 30*kHeightScale));
    }];
    self.nickNameLanel.font = [UIFont systemFontOfSize:20];
    self.nickNameLanel.textColor = [UIColor qmui_colorWithHexString:@"ffffff"];
    self.nickNameLanel.textAlignment = NSTextAlignmentCenter;
    self.nickNameLanel.text = @"昵称：晴天霹雳";

    self.cellPhonelabel = [[UILabel alloc] init];
    [_iconView addSubview:self.cellPhonelabel];
    [self.cellPhonelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_iconView.mas_bottom).with.offset(0);
        make.centerX.equalTo(_iconView);
        make.size.mas_equalTo(CGSizeMake(260*kWidthScale, 25*kHeightScale));
    }];
    self.cellPhonelabel.font = [UIFont systemFontOfSize:17];
    self.cellPhonelabel.textColor = [UIColor qmui_colorWithHexString:@"ffffff"];
    self.cellPhonelabel.textAlignment = NSTextAlignmentCenter;
    self.cellPhonelabel.text = @"手机号：18888888888";

    [self.tableView.tableHeaderView addSubview:_iconView];
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tableView.tableHeaderView.mas_top).with.offset(8);
        make.centerX.equalTo(self.tableView.tableHeaderView);
        make.size.mas_equalTo(CGSizeMake(150*kWidthScale, 130*kHeightScale));
    }];
    [_iconView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapToMyInfo:)]];
    _iconView.userInteractionEnabled = YES;

    //
    self.wavesView.imageFrame = _iconView.frame;

    [self.wavesView startWaveAnimation];
}

#pragma mark - UITableViewDataSource
// 段数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

// cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.imageView.image = [UIImage imageNamed:self.iconArray[indexPath.section]];
    cell.textLabel.text = self.titleArray[indexPath.section];
    return cell;
}

#pragma mark - UITableViewDelegate
// 段头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

// 段尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

// 段头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

// 段尾视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

// 选中某行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) { // 订单
        OrderViewController *order = [[OrderViewController alloc]init];
        order.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:order animated:YES];
    } else if (indexPath.section == 1) { // 工程
        ProjectListViewController *projectListViewController = [[ProjectListViewController alloc]init];
        projectListViewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:projectListViewController animated:YES];
    } else if (indexPath.section == 2) { // 合同
        [QMUITips showInfo:@"正在努力开发中..." inView:self.view hideAfterDelay:1];
    } else if (indexPath.section == 3) { // 设置
        PersonInfoViewController *personInfoViewController = [[PersonInfoViewController alloc]init];
        personInfoViewController.hidesBottomBarWhenPushed = YES;
        personInfoViewController.model = self.dataSourceArray[0];
        [self.navigationController pushViewController:personInfoViewController animated:YES];
    }else if (indexPath.section == 4){
        SystemViewController *systemViewController = [[SystemViewController alloc]init];
        systemViewController.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:systemViewController animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
